package com.biz.primus.app.erpbridge.trans;

import com.biz.primus.app.erpbridge.vo.oms.ErpSaleOrderState;
import com.biz.primus.app.erpbridge.vo.oms.OrderType;
import com.biz.primus.app.erpbridge.vo.oms.SaleOrder;
import com.biz.primus.common.utils.Copier;
import com.biz.primus.model.oms.enums.OrderState;
import com.biz.primus.model.oms.vo.OmsOrderReturnVo;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import org.springframework.util.Assert;

/**
 * @author yanweijin
 * @date 2017/11/26
 */
public enum SaleOrderTransformer {

	INSTANCE

	;


	public OmsOrderVo toOmsOrderVo(SaleOrder so){
		Assert.isTrue(so.getType()== OrderType.orders,"当前SaleOrder不是'orders'类型");
		OmsOrderVo vo = Copier.from(so).to(new OmsOrderVo());
		vo.setOperator(so.getChecker());
		vo.setOrderSource("pos");
		ErpSaleOrderState state = so.getState();
		if(state ==ErpSaleOrderState.outbound || state ==ErpSaleOrderState.finished){
			vo.setState(OrderState.valueOf(state.name()));
		}else{
			throw new IllegalArgumentException("不接受除已出库[outbound]和已完成[finished]以外状态的订单");
		}
		vo.setType(OrderType.orders.name());
		return vo;
	}

	public OmsOrderReturnVo toOmsOrderReturnVo(SaleOrder so){
		Assert.isTrue(so.getType()== OrderType.orders,"当前SaleOrder不是'returns'类型");
		OmsOrderReturnVo vo = Copier.from(so).to(new OmsOrderReturnVo());
		vo.setOrderSource("pos");
		return vo;
	}

	public SaleOrder formOmsOrderVo(OmsOrderVo vo){
		SaleOrder so = Copier.from(vo).to(new SaleOrder());
		so.setType(OrderType.orders);
		so.setChecker(vo.getOperator());
		so.setState(ErpSaleOrderState.valueOf(vo.getState().name()));
		return so;
	}

	public SaleOrder formOmsOrderReturnVo(OmsOrderReturnVo vo){
		SaleOrder so = Copier.from(vo).to(new SaleOrder());
		so.setType(OrderType.returns);
		so.setState(ErpSaleOrderState.for_inbound);
		return so;
	}




}
